package com.example.esp8266;

import static android.content.Context.MODE_PRIVATE;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.esp8266.databinding.SettingBottomSheetBinding;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputLayout;

/**
 * 一坨勾史
 */
public class SettingBottomFragment extends BottomSheetDialogFragment {

    private TextInputLayout productKey;
    private TextInputLayout deviceName;
    private TextInputLayout deviceSecret;
    private TextInputLayout pubTopic;
    private TextInputLayout subTopic;
    private MaterialButton set;
    private MaterialButton save;

    private MainViewModel mainViewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(R.style.BottomSheetDialogStyle, R.style.BottomSheetDialogStyle);
        mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.setting_bottom_sheet, container, false);
        productKey = (TextInputLayout) view.findViewById(R.id.productKey);
        deviceName = (TextInputLayout) view.findViewById(R.id.deviceName);
        deviceSecret = (TextInputLayout) view.findViewById(R.id.deviceSecret);
        pubTopic = (TextInputLayout) view.findViewById(R.id.pubTopic);
        subTopic = (TextInputLayout) view.findViewById(R.id.subTopic);
        set = (MaterialButton) view.findViewById(R.id.set);
        save = (MaterialButton) view.findViewById(R.id.save);


        mainViewModel.paramsWrapper.observe(requireActivity(), new Observer<MqttParamsWrapper>() {
            @Override
            public void onChanged(MqttParamsWrapper wrapper) {
                if (wrapper == null) return;
                productKey.getEditText().setText(wrapper.getProductKey());
                deviceName.getEditText().setText(wrapper.getDeviceName());
                deviceSecret.getEditText().setText(wrapper.getDeviceSecret());
                pubTopic.getEditText().setText(wrapper.getPub_topic());
                subTopic.getEditText().setText(wrapper.getSub_topic());
            }
        });

        productKey.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String dn = deviceName.getEditText().getText().toString();
                String pk = s.toString();
                pubTopic.getEditText().setText("/sys/" + pk + "/" + dn + "/thing/event/property/post");
                subTopic.getEditText().setText("/sys/" + pk + "/" + dn + "/thing/service/property/set");
            }

        });

        deviceName.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String dn = s.toString();
                String pk = productKey.getEditText().getText().toString();
                pubTopic.getEditText().setText("/sys/" + pk + "/" + dn + "/thing/event/property/post");
                subTopic.getEditText().setText("/sys/" + pk + "/" + dn + "/thing/service/property/set");
            }
        });
        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MqttParamsWrapper mqttParamsWrapper = new MqttParamsWrapper(productKey.getEditText().getText().toString(),
                        deviceName.getEditText().getText().toString(),
                        deviceSecret.getEditText().getText().toString());
                mainViewModel.initMqttUtil(mqttParamsWrapper);
                mainViewModel.paramsWrapper.postValue(mqttParamsWrapper);
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MqttParamsWrapper mqttParamsWrapper = new MqttParamsWrapper(productKey.getEditText().getText().toString(),
                        deviceName.getEditText().getText().toString(),
                        deviceSecret.getEditText().getText().toString());
                mainViewModel.putMqttParam(requireActivity().getSharedPreferences("阿巴阿巴", MODE_PRIVATE), mqttParamsWrapper);
            }
        });
        return view;
    }


//    @NonNull
//    @Override
//    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
//        Dialog dialog = super.onCreateDialog(savedInstanceState);
//        binding = SettingBottomSheetBinding.inflate(LayoutInflater.from(requireContext()));
//        dialog.setContentView(binding.getRoot());
//        ((BottomSheetDialog)dialog).getBehavior().setDraggable(true);
//        ((BottomSheetDialog)dialog).getBehavior().setState(BottomSheetBehavior.STATE_EXPANDED);
//        return dialog;
//    }
}
